<%@ page import="java.sql.*, org.json.simple.JSONObject, org.json.simple.JSONArray, support.*;" %>


<%
	String applicant = request.getParameter("applicant");
	Connection conn = null;
	PreparedStatement stm = null;
	ResultSet result = null;
	
	try{
		Class.forName("org.postgresql.Driver");
		conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/Graduate_application_database", "postgres", "postgres");
		conn.setAutoCommit(false);
		
		stm = conn.prepareStatement("SELECT name, address, country, specialization, discipline, university, title, date, gpa, location FROM degrees,applicants WHERE applicant = a_id AND applicants.a_id = ?");
		stm.setInt(1, Integer.parseInt(applicant));
	
		result = stm.executeQuery();
		conn.commit();
		conn.setAutoCommit(true);
%>

<%
		int i = 0;
        JSONObject responce = new JSONObject();
        JSONArray array = new JSONArray();
		while(result.next()){
			
			String name = result.getString(1);
			String address = result.getString(2);
			int country = result.getInt(3);
			int specialization = result.getInt(4);
			
			int discipline = result.getInt(5);
			int university = result.getInt(6);
			String title = result.getString(7);
			String date = result.getString(8);
			double gpa = result.getDouble(9);
			int location = result.getInt(10);
			
			Student student = new Student();
			student.fullName(name);
			student.address(address);
			student.citizenship(country);
			student.specialization(specialization);
			
			Degree degree = new Degree();
			degree.discipline(discipline);
			degree.university(university);
			degree.title(title);
			degree.date(date);
			degree.gpa(gpa);
			degree.location(location);
			
			String country_S = student.getCitizenshipName();
			String specialization_S = student.getSpecializationName();
		
			String discipline_S = degree.getDisciplineName();
			String university_S = degree.getUniversityName();
			String location_S = degree.getLocationName();
			
			JSONObject child = new JSONObject();
			child.put("name", name);
			child.put("address", address);
			child.put("country", country_S);
			child.put("specialization", specialization_S);
			child.put("discipline", discipline_S);
			child.put("university", university_S);
			child.put("title", title);
			child.put("date", date);
			child.put("gpa", gpa);
			child.put("location", location_S);
			
			array.add(child);
			//String degree_S = "d" + Integer.toString(i);
			//responce.put(degree_S, array);

			i++;
		}
		responce.put("degrees", array);
		out.print(responce);
		out.flush();
%>

<%

		result.close();
		stm.close();
		conn.close();
	}
	catch (Exception e){
		throw new RuntimeException(e);
	}
	finally {
		if( result != null ) {
			try{
				result.close();
			}
			catch ( SQLException e){ }
			result = null;
		}
		if ( stm != null ) {
			try {
				stm.close();
			}
			catch ( SQLException e){ }
			stm = null;
		}
		if ( conn != null){
			try{
				conn.close();
			}
			catch (SQLException e) { }
			conn = null;
		}
	}
	
%>
		
		
		
		